% This file reproduces figures and tables in Chen, Jiang, Lustig, Van Nieuwerburgh and Xiaolan, Journal of Political Economy
% This script calls Matlab execution files in the subfolders UK, US, Netherlands, and summarystats

clear; close all;

Globaloption.computation = 1;
Globaloption.savefig = 1;
Globaloption.savetablehandle = 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Descriptive Stats
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure 1a & Figure 2a & figure E.1a 
run UK\Full\Plot_UK_main.m % Note: manually click the corresponding areas of the graph to add the war labels

%% Figure 1b & Figure 2b & figure E.1b 
run US\Full\Plot_US_main.m % Note: manually click the corresponding areas of the graph to add the war labels

%% Figure 3 UK Convenience Yields
run UK\Full\Plot_UK_conv.m

%% Figure 4 US Convenience Yields
run US\post1946\Plot_ConvYield.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dynamic fiscal capacity
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure 5a UK & Figure 7
if Globaloption.computation == 1
    option.robust = 4; % benchmark result rp = 3% - mean(cy) for Figure 5(a)
    run UK\before1946\Campbell_Shiller_UK_ConvYield.m

    option.robust = 5; % cy * time-varing debt/gdp, cy after 1794 , output rp = 2%
    run UK\before1946\Campbell_Shiller_UK_ConvYield.m

    option.robust = 6; % cy after 1794, constant fraction of gdp, rp = 3%
    run UK\before1946\Campbell_Shiller_UK_ConvYield.m

    option.robust = 7; % cy after 1794, constant fraction of gdp, output rp = 2%
    run UK\before1946\Campbell_Shiller_UK_ConvYield.m

    % with debt in VAR, cy * time-varing debt/gdp, cy after 1794 , output rp = 3%,
    % Figure 7a
    run UK\before1946\Campbell_Shiller_UK_Debt_cy.m
end
% plot Figure 5a and Figure 7a+7b
run UK\before1946\Plot_Campbell_Shiller.m

%% Figure 5b US
if Globaloption.computation == 1
    run US\before1946\Campbell_Shiller_US_Benchmark.m
end
% plot Figure 5(b) US
run US\before1946\plot_campbell_shiller.m

%% Figure 6a UK & Figure E.5
if Globaloption.computation == 1
    option.robust = 1;
    run UK\post1946\Campbell_Shiller_UK_Benchmark.m
    run UK\post1946\Campbell_Shiller_UK_Debt_break.m
end
% plot Figure 6a UK & Figure E.5
run UK\post1946\Plot_CampbellShiller.m

%% Figure 6b US
if Globaloption.computation == 1
    run US\post1946\Campbell_Shiller_ConvYield.m
end
% plot Figure 6b US
run US\post1946\Plot_CampbellShiller.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Netherlands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure 8 & 9 & 10 Netherlands
% run Netherlands\Full\Plot_Holland_main.m % Note: manually click the corresponding areas of the graph to add the war labels
run Netherlands\Full\Plot_interest_rate.m

%% Figure 11a & Figure G.1
if Globaloption.computation == 1
    option.splitsample = 0;
    run Netherlands\Pre_1794\campbell_shiller_Netherland_nostock_cy.m
    run Netherlands\Pre_1794\campbell_shiller_Netherland_wstock_cy.m

    option.splitsample = 1;
    run Netherlands\Pre_1794\campbell_shiller_Netherland_nostock_cy.m
    run Netherlands\Pre_1794\campbell_shiller_Netherland_wstock_cy.m

    option.splitsample = 2;
    run Netherlands\Pre_1794\campbell_shiller_Netherland_nostock_cy.m
    run Netherlands\Pre_1794\campbell_shiller_Netherland_wstock_cy.m
end
% plot 
run Netherlands\Pre_1794\plot_campbell_shiller_netherland_cy.m

%% Figure 11b 
if Globaloption.computation == 1
    run Netherlands\code_post1815\campbell_shiller_Netherland_benchmark.m
end
% plot
run Netherlands\code_post1815\plot_campbell_shiller_netherland.m


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Appendices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure C.1 and C.2 and C.3 and C.4
run UK\before1946\Plot_CashFlowForecasts_pre.m
run UK\post1946\Plot_CashFlowForecasts_post.m

run US\before1946\Plot_CashFlowForecasts_pre.m
run US\post1946\Plot_CashFlowForecasts_post.m

%% Figure E.1
% see above with Figure 1a and 1b

%% Figure E.2 and E.3
run UK\before1946\Plot_UK_IR.m
run UK\post1946\Plot_UK_IR.m

%% Figure E.4
run UK\Full\plot_debt_return.m

%% Figure E.5
% see above with Figure 6a UK

%% Figure E.6
if Globaloption.computation == 1
   run UK\preWWI\Campbell_Shiller_UK_ConvYield_1760.m
end

run UK\preWWI\Plot_Campbell_Shiller.m

%% Figure E.7
run US\before1946\Campbell_Shiller_US_Benchmark_demeaned.m
run US\before1946_twoVAR\Campbell_Shiller_twoVAR.m
run US\before1946\plot_oneVAR_break.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Colonies
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure F.1, F.2, F.3 Colonies 
run UK\Colony\Campbell_shiller_post1850.m
run UK\Colony\plot_colony.m

%% Figure G.1
% see above with Figure 11a

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tables and Handles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Table 1
% run summarystats\SummaryStats.do

%% Table 2
% see summarystats\WACC_UK and summarystats\WACC_US

%% Table 3 and Table C.1 - C.3, Table E.1 - Table E.3 for robustness
if Globaloption.savetablehandle==1
    %UK pre-WWI
    if Globaloption.computation == 1
       option.robust = 1; % rp = 0.03-mean(cy)
       run UK\preWWI\Campbell_Shiller_UK_ConvYield.m
       run UK\preWWI\Campbell_Shiller_UK_Benchmark.m
       option.robust = 2; % rp = 0.02 -mean(cy)
       run UK\preWWI\Campbell_Shiller_UK_ConvYield.m
       run UK\preWWI\Campbell_Shiller_UK_Benchmark.m
    end

    % Column 1 of Table 3
    % Table E.1
    run UK\preWWI\Table_Basics_preWWI.m
    run UK\preWWI\Table_Estimates_preWWI_UK_partial.m

    %UK pre-WWII 1729-1946

    if Globaloption.computation == 1
       option.robust = 1; % rp = 0.03
       run UK\before1946\Campbell_Shiller_UK_Benchmark.m
       option.robust = 2; % rp = 0.02
       run UK\before1946\Campbell_Shiller_UK_Benchmark.m

       run UK\before1946\Campbell_Shiller_UK_Debt.m  % rp = 0.03
    end

    if Globaloption.computation == 1
        option.robust = 8; % cy after 1794,  constant fraction of gdp, output rp = 0.15
        run UK\before1946\Campbell_Shiller_UK_ConvYield.m

        option.robust = 9; % 4 times convenience yield
        run UK\before1946\Campbell_Shiller_UK_ConvYield.m
    end

    % Table C.1 Panel A
    run UK\before1946\Table_Basics_pre.m

    % Column 2 of Table 3
    % Table E.1
    run UK\before1946\Table_Estimates_pre_UK_partial.m
   
    % UK post-1946
    option.robust = 2; % rp = 2%
    if Globaloption.computation == 1
        run UK\post1946\Campbell_Shiller_UK_Benchmark.m
    end

    % Table C.1 Panel B
    % Column 3 of Table 3
    % Table E.1
    run UK\post1946\Table_Basics_post.m

    % US pre-WWI
    if Globaloption.computation == 1
        run US\preWWI\Campbell_Shiller_US_Benchmark.m
    end

    % Column 4 of Table 3
    run US\preWWI\Table_Basics_pre.m

    % US pre-1946
    % Column 5 of Table 3
    % Table C.2
    run US\before1946\Table_Basics_pre.m

    % US pre-1946 one VAR war moments without break in 1860 (benchmark) Table E.3
    run US\before1946\Table_nobreak_War.m

    % US pre-1946 two VAR war moments with break in 1860 Table E.3
    run US\before1946_twoVAR\Table_twoVAR_break_War.m

    % US pre-1946 moments (one VAR with different pre- and post-1860 mean）Table E.2
    run US\before1946\Table_break_pre.m

    % US pre-1946 moments (two VAR) Table E.2
    run US\before1946_twoVAR\Table_twoVAR_break_pre.m

    % US post-1946
    % Column 6 of Table 3
    % Table C.3
    run US\post1946\Campbell_Shiller.m % benchmark with no cy
    run US\post1946\Table_Basics_post.m
    run US\post1946\Campbell_Shiller_ConvYield_robust.m % 2% gdp_rp
    run US\post1946\Table_Estimates_post_US.m

    % Table 5 and Table G.4 and Table G.5
    % Netherlands Pre 1794
    if Globaloption.computation == 1
        run Netherlands\Pre_1794\campbell_shiller_Netherland_Benchmark.m
    end
    % Table G.4
    % Table 5
    run Netherlands\Pre_1794\Table_Basics_pre1794.m

    % Netherlands Post 1815
    % Table G.5
    % Table 5
    run Netherlands\code_post1815\Table_Basics_post1815.m
    
    % Table G.1 
    % run summarystats\SummaryStats.do

    % Table B.1 
    % run summarystats\cyclicality_consumption_3panel.ipynb
    
    % Handles for writing
    run UK\Full\Table_UK_sumstats.m;

    % Number for breaktest
    run US\Full\breaktest.m


end